package com.netposa.datawarehouse.aggregator.search;

import com.netposa.technicaltactics.client.VehicleSearchImageClient;
import com.netposa.technicaltactics.common.NewVehicleSearchCondition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.RecursiveTask;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class VehicleImageSearchTask extends RecursiveTask {
   private static final Logger log = LoggerFactory.getLogger(VehicleImageSearchTask.class);
   private NewVehicleSearchCondition newVehicleSearchCondition;
   @Autowired
   private VehicleSearchImageClient vehicleSearchImageClient;
   private Long vehicleTimeLength;
   private Long startTime;
   private Long endTime;

   public VehicleImageSearchTask(NewVehicleSearchCondition newVehicleSearchCondition, VehicleSearchImageClient vehicleSearchImageClient, Long vehicleTimeLength, Long startTime, Long endTime) {
      this.newVehicleSearchCondition = newVehicleSearchCondition;
      this.vehicleSearchImageClient = vehicleSearchImageClient;
      this.vehicleTimeLength = vehicleTimeLength;
      this.startTime = startTime;
      this.endTime = endTime;
   }

   protected List compute() {
      List result = new ArrayList();
      String timeFormat = "yyyyMMdd";
      if (this.endTime - this.startTime == this.vehicleTimeLength - 1000L) {
         String start = DateFormatUtils.format(this.startTime, timeFormat);
         String end = DateFormatUtils.format(this.endTime, timeFormat);
         NewVehicleSearchCondition vehicleSearchCondition = new NewVehicleSearchCondition();
         BeanUtils.copyProperties(this.newVehicleSearchCondition, vehicleSearchCondition);
         vehicleSearchCondition.setVehicleStartTimeStr(start);
         vehicleSearchCondition.setVehicleEndTimeStr(end);

         try {
            result.addAll(this.vehicleSearchImageClient.searchByImgNew(vehicleSearchCondition));
         } catch (TException var7) {
            log.error("车辆以图搜图异常：", var7);
         }
      } else {
         Long transitionTime = this.startTime + this.vehicleTimeLength;
         log.info("startTime:" + this.startTime + ",vehicleTimeLength:" + this.vehicleTimeLength + ",transitionTime:" + transitionTime);
         VehicleImageSearchTask leftTask = new VehicleImageSearchTask(this.newVehicleSearchCondition, this.vehicleSearchImageClient, this.vehicleTimeLength, this.startTime, transitionTime - 1000L);
         VehicleImageSearchTask rightTask = new VehicleImageSearchTask(this.newVehicleSearchCondition, this.vehicleSearchImageClient, this.vehicleTimeLength, transitionTime, this.endTime);
         leftTask.fork();
         rightTask.fork();
         result.addAll((Collection)leftTask.join());
         result.addAll((Collection)rightTask.join());
      }

      return result;
   }
}
